package com.designpatternspart2.behavioraldesignpatterns.observerpattern;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 *
 * @author Lance
 */
public class WeatherStation implements WeatherSubject
{

    private Set<WeatherObserver> weatherObservers;
    private int temperature;

    public WeatherStation(int temperature)
    {
        weatherObservers = new HashSet<WeatherObserver>();
        this.temperature = temperature;
    }

    @Override
    public void addObserver(WeatherObserver weatherObserver)
    {
        getWeatherObservers().add(weatherObserver);
    }

    @Override
    public void removeObserver(WeatherObserver weatherObserver)
    {
        getWeatherObservers().remove(weatherObserver);
    }

    @Override
    public void doNotify()
    {
        Iterator<WeatherObserver> it = getWeatherObservers().iterator();
        while (it.hasNext())
        {
            WeatherObserver weatherObserver = it.next();
            weatherObserver.doUpdate(getTemperature());
        }
    }

    public void setTemperature(int newTemperature)
    {
        System.out.println("\nWeather station setting temperature to " + newTemperature);
        temperature = newTemperature;
        doNotify();
    }

    public int getTemperature()
    {
        return this.temperature;
    }

    public Set<WeatherObserver> getWeatherObservers()
    {
        return weatherObservers;
    }

    public void setWeatherObservers(Set<WeatherObserver> weatherObservers)
    {
        this.weatherObservers = weatherObservers;
    }
}